// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: random_id.hpp 212 2008-08-28 12:16:26Z heavyzheng $
//

#ifndef ZZHENG_UTILITY_RANDOM_ID_HPP_20080710__
#define ZZHENG_UTILITY_RANDOM_ID_HPP_20080710__

#include <zzheng/config.hpp>

#include <string>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

namespace zzheng {

    //! \addtogroup utility zzheng.utility
    //! \{

    //! This class is a random ID generator.
    template<unsigned int Length>
    class random_id {

        enum length_t {
            length = Length,
        };

    public:

        explicit random_id() {
            static const int FIRST_RANDOM_NUMBER = seed();
        }

        //! Returns a random ID string of the given size.
        std::string get() {
            const unsigned int mod_base = 0x100;
            const unsigned int max_loop = (length >= 2 ? (length / 2) : 1);
            std::ostringstream oss;
            for (unsigned int i = 0; i < max_loop; ++i) {
                int num = std::rand() % mod_base;
                oss << std::setw(2) << std::setfill('0') << std::setbase(16) << num;
            }
            return oss.str();
        }

    private:

        static int seed() {
            std::srand(static_cast<unsigned int>(std::time(0)));
            return std::rand();
        }

    }; // class random_id

    typedef random_id<4 > random_id_4 ;
    typedef random_id<8 > random_id_8 ;
    typedef random_id<16> random_id_16;
    typedef random_id<32> random_id_32;

    //! \} end group utility

} // namespace zzheng

#endif // ZZHENG_UTILITY_RANDOM_ID_HPP_20080710__


